using hao_Dto.User;
using hao_Model;
using hao_Reppsitory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace User.Service.Role
{
    public class RoleService : IRoleService
    {
        private readonly IRoleRepository _roleRepository;
        
        public RoleService(IRoleRepository roleRepository)
        {
            _roleRepository = roleRepository;
        }
        
        public async Task<List<RoleDto>> GetAllRolesAsync()
        {
            var roles = await _roleRepository.GetAllAsync();
            return roles.Select(ToRoleDto).ToList();
        }
        
        /// <summary>
        /// 分页获取角色列表
        /// </summary>
        public async Task<(List<RoleDto> Roles, int TotalCount)> GetRolesPagedAsync(RolePagedQueryDto queryDto)
        {
            // 如果有搜索关键词，需要特殊处理
            if (!string.IsNullOrWhiteSpace(queryDto.Keyword))
            {
                // 先获取所有角色，然后在内存中过滤（简单实现）
                var allRoles = await _roleRepository.GetAllAsync();
                var filteredRoles = allRoles.Where(r => r.Name.Contains(queryDto.Keyword, StringComparison.OrdinalIgnoreCase))
                                          .ToList();
                
                var totalCount = filteredRoles.Count;
                var pagedRoles = filteredRoles
                    .Skip((queryDto.PageNumber - 1) * queryDto.PageSize)
                    .Take(queryDto.PageSize)
                    .Select(ToRoleDto)
                    .ToList();
                    
                return (pagedRoles, totalCount);
            }
            else
            {
                // 直接使用仓储的分页查询
                var (items, totalCount) = await _roleRepository.GetPagedAsync(
                    queryDto.PageNumber, 
                    queryDto.PageSize, 
                    queryDto.OrderBy, 
                    queryDto.IsDescending);
                    
                var roles = items.Select(ToRoleDto).ToList();
                return (roles, totalCount);
            }
        }
        
        public async Task<RoleDto> GetRoleByIdAsync(Guid roleId)
        {
            var role = await _roleRepository.GetByIdAsync(roleId);
            return role != null ? ToRoleDto(role) : null;
        }
        
        public async Task<RoleDto> CreateRoleAsync(CreateRoleDto createRoleDto)
        {
            var role = new hao_Model.Role
            {
                Id = Guid.NewGuid(),
                Name = createRoleDto.Name,
                Description = createRoleDto.Description,
                IsDeleted = false
            };
            
            await _roleRepository.AddAsync(role);
            return ToRoleDto(role);
        }
        
        public async Task<RoleDto> UpdateRoleAsync(Guid roleId, UpdateRoleDto updateRoleDto)
        {
            var role = await _roleRepository.GetByIdAsync(roleId);
            if (role == null) return null;
            
            role.Name = updateRoleDto.Name;
            role.Description = updateRoleDto.Description;
            
            await _roleRepository.UpdateAsync(role);
            return ToRoleDto(role);
        }
        
        public async Task DeleteRoleAsync(Guid roleId)
        {
            await _roleRepository.DeleteAsync(roleId);
        }
        
        // 映射方法
        private RoleDto ToRoleDto(hao_Model.Role role) => new RoleDto
        {
            Id = role.Id,
            Name = role.Name,
            Description = role.Description
        };
    }
}